package com.qf.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired  //密码加密器
    private PasswordEncoder passwordEncoder;
    @Autowired
    private UserDetailsService userDetailsService;

    @Override//用来配置用户名和密码
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //通过业务类来进行认证
        auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder);
    }

    @Override//配置资源请求拦截或放行规则
    protected void configure(HttpSecurity http) throws Exception {
        //http.cors().disable();//

        http.csrf().disable(); //关闭默认资源拦截！！！必须

        //自定义登录页面
        http.formLogin()
                .loginPage("/login.html")  //设置登录页面
                .loginProcessingUrl("/user/login")  //设置登录路径
                .defaultSuccessUrl("/index.html")   //默认登录成功跳转路径
                //   .usernameParameter("name") 如果不是设置的不是默认的username/password 要在这里修改
                //   .passwordParameter("password")
                .permitAll(); //放行操作




        //认证通过之后全部放行（不拦截）必须
        http.authorizeRequests()
                .anyRequest()
                .authenticated();

    }
}






